package com.tengju.bff.interfaces.covercharge.listener;

import com.tengju.bff.interfaces.covercharge.service.IServiceSelfCertificationWithdrawService;
import com.tengju.bff.interfaces.covercharge.service.IServiceContractService;
import com.tengju.bff.interfaces.covercharge.service.IServiceWithdarwCycleAddService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author liang_shi
 * @date 2020/7/16 11:23
 * @description 消息监听接受服务
 */
@Slf4j
@Component
public class MqMessageNotifyReceiver {

    @Autowired
    private IServiceSelfCertificationWithdrawService serviceSelfCertificationWithdrawService;

    @Autowired
    private IServiceContractService serviceContractService;

    @Autowired
    private IServiceWithdarwCycleAddService serviceWithdarwCycleAddService;

    /**
     * 用户证书申请消息监听-队列消费
     * @param message 内容
     * @param channel 消息管道
     * @param headers 消息头
     */
//    @RabbitListener(queues = MqMessageConstants.SERVICE_CERT_APPLY_NOTIFY)
//    public void listenerCertApplyMessage(@Payload String message, Channel channel,
//                                         @Headers Map<String, Object> headers) {
//        final Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
//        log.info("监听到用户证书申请消息,开始消费,message:{},消费端 deliveryTag :{}", message, headers.get(AmqpHeaders.DELIVERY_TAG).toString());
//        // 执行消费逻辑
//        RabbitMqMessage rabbitMqMessage = JSON.parseObject(message, RabbitMqMessage.class);
//        serviceContractService.dealMqCertApplyMessage(deliveryTag, channel, rabbitMqMessage);
//    }


    /**
     * 周期打款-队列消费
     *
     * @param message 内容
     * @param channel 消息管道
     * @param headers 消息头
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = MqMessageConstants.SERVICE_CYCLE_ONLINE_PAY_MONEY_NOTIFY, durable = "true"),
//            exchange = @Exchange(value = MqMessageConstants.SERVICE_CYCLE_ONLINE_PAY_MONEY_NOTIFY, durable = "true", type = "direct", ignoreDeclarationExceptions = "true"),
//            key = MqMessageConstants.SERVICE_CYCLE_ONLINE_PAY_MONEY_CALLBACK
//    ))
//    @RabbitHandler
//    public void listenerCycleOnlinePayMoneyMessage(@Payload String message, Channel channel,
//                                                   @Headers Map<String, Object> headers) {
//
//        final Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
//        log.info("[服务费后台][个人收益打款代发][监听周期打款消息,开始消费],message:{},消费端 deliveryTag :{}", message, headers.get(AmqpHeaders.DELIVERY_TAG).toString());
//        // 执行消费逻辑
//        RabbitMqMessage rabbitMqMessage = JSON.parseObject(message, RabbitMqMessage.class);
//        try {
//            serviceWithdarwCycleAddService.dealOnlinePayMoneyMessage(deliveryTag, channel, rabbitMqMessage);
//        }catch (Exception e){
//            log.error("[服务费后台][个人收益打款申请记录][MQ打款失败,有异常抛出]rabbitMqMessage{},message{}", rabbitMqMessage,e);
//        }
//    }

    /**
     * 自认证打款结果通知
     *
     * @param message 内容
     * @param channel 消息管道
     * @param headers 消息头
     */
//    @RabbitListener(queues = MqMessageConstants.SERVICE_CERTIFICATION_MODEL_RESULTS_NOTICE)
//    @RabbitHandler
//    public void listenerCertificationModelResultsNotice(@Payload String message, Channel channel,
//                                                   @Headers Map<String, Object> headers) {
//
//        final Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
//        log.info("[服务费后台][自认证打款结果通知][监听周期打款消息,开始消费],message:{},消费端 deliveryTag :{}", message, headers.get(AmqpHeaders.DELIVERY_TAG).toString());
//        // 执行消费逻辑
//        TransferResponse rabbitMqMessage = null;
//        try {
//            rabbitMqMessage = JSON.parseObject(message, TransferResponse.class);
//            CashBackRes cashBackRes = serviceSelfCertificationWithdrawService.certificationModelResultsNotice(rabbitMqMessage);
//        } catch (Exception e) {
//            MsgSendUtil.sendDingMsg("自认证打款结果通知, 收到mq出现异常{}", rabbitMqMessage);
//            log.error("自认证打款结果通知, 收到mq出现异常{}", rabbitMqMessage);
//        } finally {
//            try {
//                channel.basicAck(deliveryTag, true);
//            } catch (IOException e) {
//                log.error("自认证打款结果通知, mq确认出现异常", e);
//
//                MsgSendUtil.sendDingMsg("自认证打款结果通知, mq确认出现异常{}", e);
//            }
//        }
//
//    }



}
